package com.system.cacheCenterBackend.controller.redisDataController;

import com.system.cacheCenterBackend.service.cacheService.RedisCacheService;
import com.system.cacheClient.common.CacheR;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Redis数据类型操作控制器
 * 提供List、Set、Hash、ZSet等数据类型的RESTful API接口
 */
@RestController
@RequestMapping("/redis")
@RequiredArgsConstructor
public class RedisDataTypeController {

    private final RedisCacheService redisCacheService;

    // ==================== List 操作 ====================

    /**
     * 向列表左侧添加元素
     */
    @PostMapping("/list/{key}/left")
    public CacheR<Long> leftPush(@PathVariable String key, @RequestBody String value) {
        try {
            Long result = redisCacheService.leftPush(key, value);
            return CacheR.success(result, "左侧添加元素成功");
        } catch (Exception e) {
            return CacheR.fail("左侧添加元素失败: " + e.getMessage());
        }
    }

    /**
     * 向列表右侧添加元素
     */
    @PostMapping("/list/{key}/right")
    public CacheR<Long> rightPush(@PathVariable String key, @RequestBody String value) {
        try {
            Long result = redisCacheService.rightPush(key, value);
            return CacheR.success(result, "右侧添加元素成功");
        } catch (Exception e) {
            return CacheR.fail("右侧添加元素失败: " + e.getMessage());
        }
    }

    /**
     * 从列表左侧弹出元素
     */
    @DeleteMapping("/list/{key}/left")
    public CacheR<String> leftPop(@PathVariable String key) {
        try {
            String result = redisCacheService.leftPop(key);
            if (result != null) {
                return CacheR.success(result, "左侧弹出元素成功");
            } else {
                return CacheR.fail(404, "列表为空或不存在");
            }
        } catch (Exception e) {
            return CacheR.fail("左侧弹出元素失败: " + e.getMessage());
        }
    }

    /**
     * 从列表右侧弹出元素
     */
    @DeleteMapping("/list/{key}/right")
    public CacheR<String> rightPop(@PathVariable String key) {
        try {
            String result = redisCacheService.rightPop(key);
            if (result != null) {
                return CacheR.success(result, "右侧弹出元素成功");
            } else {
                return CacheR.fail(404, "列表为空或不存在");
            }
        } catch (Exception e) {
            return CacheR.fail("右侧弹出元素失败: " + e.getMessage());
        }
    }

    /**
     * 获取列表指定范围的元素
     */
    @GetMapping("/list/{key}/range")
    public CacheR<List<String>> listRange(@PathVariable String key, 
                                         @RequestParam(defaultValue = "0") long start,
                                         @RequestParam(defaultValue = "-1") long end) {
        try {
            List<String> result = redisCacheService.listRange(key, start, end);
            return CacheR.success(result, "获取列表范围元素成功");
        } catch (Exception e) {
            return CacheR.fail("获取列表范围元素失败: " + e.getMessage());
        }
    }

    /**
     * 获取列表长度
     */
    @GetMapping("/list/{key}/size")
    public CacheR<Long> listSize(@PathVariable String key) {
        try {
            Long result = redisCacheService.listSize(key);
            return CacheR.success(result, "获取列表长度成功");
        } catch (Exception e) {
            return CacheR.fail("获取列表长度失败: " + e.getMessage());
        }
    }

    // ==================== Set 操作 ====================

    /**
     * 向集合添加元素
     */
    @PostMapping("/set/{key}")
    public CacheR<Long> setAdd(@PathVariable String key, @RequestBody List<String> values) {
        try {
            Long result = redisCacheService.setAdd(key, values.toArray(new String[0]));
            return CacheR.success(result, "集合添加元素成功");
        } catch (Exception e) {
            return CacheR.fail("集合添加元素失败: " + e.getMessage());
        }
    }

    /**
     * 从集合移除元素
     */
    @DeleteMapping("/set/{key}")
    public CacheR<Long> setRemove(@PathVariable String key, @RequestBody List<String> values) {
        try {
            Long result = redisCacheService.setRemove(key, values.toArray(new String[0]));
            return CacheR.success(result, "集合移除元素成功");
        } catch (Exception e) {
            return CacheR.fail("集合移除元素失败: " + e.getMessage());
        }
    }

    /**
     * 获取集合所有元素
     */
    @GetMapping("/set/{key}")
    public CacheR<Set<String>> setMembers(@PathVariable String key) {
        try {
            Set<String> result = redisCacheService.setMembers(key);
            return CacheR.success(result, "获取集合元素成功");
        } catch (Exception e) {
            return CacheR.fail("获取集合元素失败: " + e.getMessage());
        }
    }

    /**
     * 检查元素是否在集合中
     */
    @GetMapping("/set/{key}/member/{value}")
    public CacheR<Boolean> setIsMember(@PathVariable String key, @PathVariable String value) {
        try {
            Boolean result = redisCacheService.setIsMember(key, value);
            return CacheR.success(result, "检查集合成员成功");
        } catch (Exception e) {
            return CacheR.fail("检查集合成员失败: " + e.getMessage());
        }
    }

    /**
     * 获取集合大小
     */
    @GetMapping("/set/{key}/size")
    public CacheR<Long> setSize(@PathVariable String key) {
        try {
            Long result = redisCacheService.setSize(key);
            return CacheR.success(result, "获取集合大小成功");
        } catch (Exception e) {
            return CacheR.fail("获取集合大小失败: " + e.getMessage());
        }
    }

    // ==================== Hash 操作 ====================

    /**
     * 设置哈希字段值
     */
    @PostMapping("/hash/{key}/{field}")
    public CacheR<Void> hashPut(@PathVariable String key, @PathVariable String field, @RequestBody String value) {
        try {
            redisCacheService.hashPut(key, field, value);
            return CacheR.success(null, "设置哈希字段成功");
        } catch (Exception e) {
            return CacheR.fail("设置哈希字段失败: " + e.getMessage());
        }
    }

    /**
     * 获取哈希字段值
     */
    @GetMapping("/hash/{key}/{field}")
    public CacheR<String> hashGet(@PathVariable String key, @PathVariable String field) {
        try {
            String result = redisCacheService.hashGet(key, field);
            if (result != null) {
                return CacheR.success(result, "获取哈希字段成功");
            } else {
                return CacheR.fail(404, "哈希字段不存在");
            }
        } catch (Exception e) {
            return CacheR.fail("获取哈希字段失败: " + e.getMessage());
        }
    }

    /**
     * 删除哈希字段
     */
    @DeleteMapping("/hash/{key}")
    public CacheR<Long> hashDelete(@PathVariable String key, @RequestBody List<String> fields) {
        try {
            Long result = redisCacheService.hashDelete(key, fields.toArray(new String[0]));
            return CacheR.success(result, "删除哈希字段成功");
        } catch (Exception e) {
            return CacheR.fail("删除哈希字段失败: " + e.getMessage());
        }
    }

    /**
     * 获取哈希所有字段和值
     */
    @GetMapping("/hash/{key}")
    public CacheR<Map<String, String>> hashGetAll(@PathVariable String key) {
        try {
            Map<String, String> result = redisCacheService.hashGetAll(key);
            return CacheR.success(result, "获取哈希所有字段成功");
        } catch (Exception e) {
            return CacheR.fail("获取哈希所有字段失败: " + e.getMessage());
        }
    }

    /**
     * 检查哈希字段是否存在
     */
    @GetMapping("/hash/{key}/{field}/exists")
    public CacheR<Boolean> hashExists(@PathVariable String key, @PathVariable String field) {
        try {
            Boolean result = redisCacheService.hashExists(key, field);
            return CacheR.success(result, "检查哈希字段存在性成功");
        } catch (Exception e) {
            return CacheR.fail("检查哈希字段存在性失败: " + e.getMessage());
        }
    }

    /**
     * 获取哈希字段数量
     */
    @GetMapping("/hash/{key}/size")
    public CacheR<Long> hashSize(@PathVariable String key) {
        try {
            Long result = redisCacheService.hashSize(key);
            return CacheR.success(result, "获取哈希字段数量成功");
        } catch (Exception e) {
            return CacheR.fail("获取哈希字段数量失败: " + e.getMessage());
        }
    }

    // ==================== ZSet 操作 ====================

    /**
     * 向有序集合添加元素
     */
    @PostMapping("/zset/{key}")
    public CacheR<Boolean> zsetAdd(@PathVariable String key, 
                                  @RequestParam String value, 
                                  @RequestParam double score) {
        try {
            Boolean result = redisCacheService.zsetAdd(key, value, score);
            return CacheR.success(result, "有序集合添加元素成功");
        } catch (Exception e) {
            return CacheR.fail("有序集合添加元素失败: " + e.getMessage());
        }
    }

    /**
     * 从有序集合移除元素
     */
    @DeleteMapping("/zset/{key}")
    public CacheR<Long> zsetRemove(@PathVariable String key, @RequestBody List<String> values) {
        try {
            Long result = redisCacheService.zsetRemove(key, values.toArray(new String[0]));
            return CacheR.success(result, "有序集合移除元素成功");
        } catch (Exception e) {
            return CacheR.fail("有序集合移除元素失败: " + e.getMessage());
        }
    }

    /**
     * 获取有序集合指定范围的元素
     */
    @GetMapping("/zset/{key}/range")
    public CacheR<Set<String>> zsetRange(@PathVariable String key, 
                                        @RequestParam(defaultValue = "0") long start,
                                        @RequestParam(defaultValue = "-1") long end) {
        try {
            Set<String> result = redisCacheService.zsetRange(key, start, end);
            return CacheR.success(result, "获取有序集合范围元素成功");
        } catch (Exception e) {
            return CacheR.fail("获取有序集合范围元素失败: " + e.getMessage());
        }
    }

    /**
     * 获取有序集合指定分数范围的元素
     */
    @GetMapping("/zset/{key}/rangeByScore")
    public CacheR<Set<String>> zsetRangeByScore(@PathVariable String key, 
                                               @RequestParam double minScore,
                                               @RequestParam double maxScore) {
        try {
            Set<String> result = redisCacheService.zsetRangeByScore(key, minScore, maxScore);
            return CacheR.success(result, "按分数获取有序集合元素成功");
        } catch (Exception e) {
            return CacheR.fail("按分数获取有序集合元素失败: " + e.getMessage());
        }
    }

    /**
     * 获取元素在有序集合中的分数
     */
    @GetMapping("/zset/{key}/score/{value}")
    public CacheR<Double> zsetScore(@PathVariable String key, @PathVariable String value) {
        try {
            Double result = redisCacheService.zsetScore(key, value);
            if (result != null) {
                return CacheR.success(result, "获取元素分数成功");
            } else {
                return CacheR.fail(404, "元素不存在");
            }
        } catch (Exception e) {
            return CacheR.fail("获取元素分数失败: " + e.getMessage());
        }
    }

    /**
     * 获取有序集合大小
     */
    @GetMapping("/zset/{key}/size")
    public CacheR<Long> zsetSize(@PathVariable String key) {
        try {
            Long result = redisCacheService.zsetSize(key);
            return CacheR.success(result, "获取有序集合大小成功");
        } catch (Exception e) {
            return CacheR.fail("获取有序集合大小失败: " + e.getMessage());
        }
    }
}